#include <PA9.h>       // Include for PA_Lib
#include <fat.h>
#include <stdlib.h>
#include "clirc.h"
#include "gtouch.h"
#include "wificonf.h"
#include "config.h"
#include "sounds.h"
#include "irc.h"
#include "ircproto.h"
#include "winsys.h"
#include "dialogs.h"
#include "skeyb.h"
#include "iconfig.h"
#include "gfx/font/all_gfx.c"
#include "gfx/font/all_gfx.h"
#ifdef RUSSIAN
#include "gfx/main/all_gfx.c"
#include "gfx/main/all_gfx.h"
#else
#include "gfx.eng/main/all_gfx.c"
#include "gfx.eng/main/all_gfx.h"
#endif

u32 SystemTicks = 0;
u8 DSLBrightness = PersonalData->_user_data.defaultBrightness;
extern WS_struct* IRC_MainStatusWindow;
extern u8 Config_SmoothScroll;
u8 NoSmoothScroll = 0;
u8 ScrollHeldTime = 0;
u8 Config_LeftHanded = 0;
u32 UTicks = 0;
u8 wfs = 0;
int GetWifiSignal(int num) {
   Wifi_AccessPoint ap;
	Wifi_GetAPData(num,&ap);
	return ((ap.rssi*100)/0xD0);
}
void UpdateWifiSignal()
{

	int rssi=GetWifiSignal(0);
	if(rssi<20) {
		PA_SetSpriteAnim(0, 115, 0);
	}
	else if(rssi<40) {
		PA_SetSpriteAnim(0, 115, 1);
	}
	else if(rssi<60) {
		PA_SetSpriteAnim(0, 115, 2);
	}
	else {
	   PA_SetSpriteAnim(0, 115, 3);
	}
	UTicks = 0;

}
void ShowWifiSignal()
{
   PA_LoadSpritePal(0, 15, (void*)Signal_Pal);	
	PA_CreateSprite(0, 115, (void*)Signal_Sprite, OBJ_SIZE_16X16, 1, 15, 22, 1);
	wfs = 1;
}	

void DeleteWifiSignal()
{
   PA_DeleteSprite(0, 115);
   wfs = 0;
   
} 
void MyWaitForVBL()
{
	PA_WaitForVBL();
	AS_SoundVBL();
	if ((IRC_MainStatusWindow != NULL) && (!PadHeld('R')))
	{
		if ((PadHeld('u') || PadHeld('d')) && (ScrollHeldTime < 255)) ScrollHeldTime++;
		if (!(PadHeld('u') || PadHeld('d'))) ScrollHeldTime = 0;
		if (ScrollHeldTime < 120)
		{
		   if (NoSmoothScroll)
		   {
				Config_SmoothScroll = 1;
				NoSmoothScroll = 0;
			}
		   if (Config_SmoothScroll)
		   {
			   if (PadHeld('u')) WS_ScrollUp();
			   if (PadHeld('d')) WS_ScrollDown();
			} else {
			   if (PadHeld('u')) WS_ScrollUp();
			   if (PadHeld('d')) WS_ScrollDown();
			}
		} else {
		   if (Config_SmoothScroll && !NoSmoothScroll)
		   {
				Config_SmoothScroll = 0;
				NoSmoothScroll = 1;
			}   
		   if (PadHeld('u') && (SystemTicks % 2 == 0)) WS_ScrollUp();
		   if (PadHeld('d') && (SystemTicks % 2 == 0)) WS_ScrollDown();
		}		
	} else ScrollHeldTime = 0;
	WS_Update();
	IRC_Update();	
	if (PA_LidClosed())
	{
		PA_SetScreenLight(0, 0);
		PA_SetScreenLight(1, 0);
	} else {
		PA_SetScreenLight(0, 1);
		PA_SetScreenLight(1, 1);
	}
	if (Pad.Newpress.Select)
	{
		DSLBrightness++;
		if (DSLBrightness > 3) DSLBrightness = 0;
		PA_SetDSLBrightness(DSLBrightness);
	}
	if (!PadHeld('R') && PadNewpress('r')) IRC_SwitchWindow(1);
	if (!PadHeld('R') && PadNewpress('l')) IRC_SwitchWindow(0);
	SystemTicks++;
   if ((wfs = 1)) {
	   if (UTicks >= 600) {
			UpdateWifiSignal();
		} else {
	   	UTicks++;
		}			
	}	
}

void BrightnessDown()
{
	for (s8 bright=32;bright>=0;bright-=2)
	{
	   PA_SetBrightness(0, bright);
	   MyWaitForVBL();
	} 
}

void BrightnessUp()
{
	for (s8 bright=0;bright<=32;bright+=2)
	{
	   PA_SetBrightness(0, bright);
	   MyWaitForVBL();
	} 
}

void ShowAbout()
{
   PA_LoadTiledBg(0, 2, AboutBg);
   BrightnessDown();
   while ((!Pad.Held.Anykey) && (!Stylus.Newpress)) MyWaitForVBL();
	BrightnessUp();
}   

// Function: main()
int main(int argc, char ** argv)
{
	PA_Init();    // Initializes PA_Lib
	PA_InitVBL(); // Initializes a standard VBL
	PA_SetBrightness(0, 31);
	PA_SetBrightness(1, 31);
	AS_Init(AS_MODE_MP3 /*| AS_MODE_SURROUND*/ | AS_MODE_8CH);
   AS_SetDefaultSettings(AS_PCM_16BIT, 11025, 0 /*AS_SURROUND*/);
	PA_InitWifi();
	PA_InitCustomText(1, 1, MyText);
	PA_InitCustomText(0, 1, MyText);
	PA_SetAutoCheckLid(0);
	GTouchInit();
	IRC_Init();
	EmptyClipboard();

	PA_LoadTiledBg(1, 2, Title);
	PA_LoadTiledBg(0, 2, MainMenu);
	PA_SetTextTileCol(1, 9);
	PA_OutputText(1, 0, 23, "v%s", VERSION);
	PA_LoadSpritePal(1, 0, (void*)Heart_Pal);
	for (s8 bright=32;bright>=0;bright-=2)
	{
//	   PA_SetBrightness(0, bright);
	   PA_SetBrightness(1, bright);
	   MyWaitForVBL();
	} 
//	for (u8 i = 0;((i < 180) && (!Pad.Newpress.Anykey) && (!Stylus.Newpress)); i++) MyWaitForVBL();

	Config_LoadConfig();
	s8 LetterYpos[5] = {-2, -1, 0, 1, 2};
	s8 LetterYsign[5] = {1, -1, 1, -1, 1};
	s8 LetterXpos[5] = {-1, 0, 1, -1, 0};
	s8 LetterXsign[5] = {1, -1, 1, -1, 1};
	const u8 ClircLetterX[5] = {5, 55, 105, 155, 205};
	const u8 ClircLettersY = 5;

	while (1)
	{
	   wfs = 0;
	   DeleteWifiSignal();
		PA_LoadTiledBg(0, 2, MainMenu);
		PA_LoadSpritePal(0, 0, (void*)ClircLetters_Pal);
		PA_CreateSprite(0, 0, (void*)LetterC_Sprite, OBJ_SIZE_64X64, 1, 0, ClircLetterX[0]+LetterXpos[0], ClircLettersY+LetterYpos[0]);
		PA_CreateSprite(0, 1, (void*)LetterL_Sprite, OBJ_SIZE_64X64, 1, 0, ClircLetterX[1]+LetterXpos[1], ClircLettersY+LetterYpos[1]);
		PA_CreateSprite(0, 2, (void*)LetterI_Sprite, OBJ_SIZE_64X64, 1, 0, ClircLetterX[2]+LetterXpos[2], ClircLettersY+LetterYpos[2]);
		PA_CreateSprite(0, 3, (void*)LetterR_Sprite, OBJ_SIZE_64X64, 1, 0, ClircLetterX[3]+LetterXpos[3], ClircLettersY+LetterYpos[3]);
		PA_CreateSprite(0, 4, (void*)LetterC_Sprite, OBJ_SIZE_64X64, 1, 0, ClircLetterX[4]+LetterXpos[4], ClircLettersY+LetterYpos[4]);

		if (IRC_MainStatusWindow == NULL)
		{
			PA_LoadSpritePal(1, 0, (void*)Heart_Pal);
			PA_CreateSprite(1, 0, (void*)Heart_Sprite, OBJ_SIZE_16X16, 1, 0, 82, 192);
			PA_CreateSprite(1, 1, (void*)Heart_Sprite, OBJ_SIZE_16X16, 1, 0, 106, 192);
			PA_CreateSprite(1, 2, (void*)Heart_Sprite, OBJ_SIZE_16X16, 1, 0, 133, 192);
			PA_CreateSprite(1, 3, (void*)Heart_Sprite, OBJ_SIZE_16X16, 1, 0, 157, 192);
		}
		PA_EnableSpecialFx(0, SFX_ALPHA, 0, SFX_BG0 | SFX_BG1 | SFX_BG2 | SFX_BG3 | SFX_BD);
		PA_SetSFXAlpha(0, 16, 0);
		u8 AlphaLevel = 0;
		s8 AlphaSign = 0;

		BrightnessDown();

		int iRes = 0;
		while (!iRes)
		{
		   u32 HeartTime = 0;
			while (!GTouch())
			{
				if (IRC_MainStatusWindow == NULL)
				{
				   HeartTime++;
				   if (HeartTime == 1800) PA_SetSpriteY(1, 0, 112);
				   if (HeartTime == 1805) PA_SetSpriteY(1, 1, 104);
				   if (HeartTime == 1810) PA_SetSpriteY(1, 2, 104);
				   if (HeartTime == 1815) PA_SetSpriteY(1, 3, 112);
				   if (HeartTime == 1820) PA_SetSpriteY(1, 0, 192);
				   if (HeartTime == 1825) PA_SetSpriteY(1, 1, 192);
				   if (HeartTime == 1830) PA_SetSpriteY(1, 2, 192);
				   if (HeartTime == 1835) PA_SetSpriteY(1, 2, 104);
				   if (HeartTime == 1840) PA_SetSpriteY(1, 1, 104);
				   if (HeartTime == 1845) PA_SetSpriteY(1, 0, 112);
				   if (HeartTime == 1850) PA_SetSpriteY(1, 3, 192);
				   if (HeartTime == 1855) PA_SetSpriteY(1, 2, 192);
				   if (HeartTime == 1860) PA_SetSpriteY(1, 1, 192);
				   if (HeartTime == 1865) PA_SetSpriteY(1, 0, 192);
				   if (HeartTime > 1865) HeartTime = 0;
				}
				if (SystemTicks % 2 == 0)
				{
					for (u8 Letter = 0; Letter < 5; Letter++)
					{
						LetterYpos[Letter] += LetterYsign[Letter];
						if (LetterYpos[Letter] > 2) LetterYsign[Letter] = -1;
						if (LetterYpos[Letter] < -2) LetterYsign[Letter] = 1;
						LetterXpos[Letter] += LetterXsign[Letter];
						if (LetterXpos[Letter] > 1) LetterXsign[Letter] = -1;
						if (LetterXpos[Letter] < -1) LetterXsign[Letter] = 1;
						PA_SetSpriteXY(0, Letter, ClircLetterX[Letter]+LetterXpos[Letter], ClircLettersY+LetterYpos[Letter]);
					}
					if (AlphaLevel == 8) AlphaSign = -1;
					if (AlphaLevel == 0) AlphaSign = 0;
					AlphaLevel += AlphaSign;
					PA_SetSFXAlpha(0, 16-AlphaLevel, AlphaLevel);
				}
				if (SystemTicks % 30 == 0)
				{
					for (u8 Letter = 0; Letter < 5; Letter++)
					{
						PA_SetSpriteMode(0, Letter, 0);
					}
					PA_SetSpriteMode(0, rand() % 5, 1);
					AlphaLevel = 1;
					AlphaSign = 1;
				}
				MyWaitForVBL();
			}			
			if (GRectTouched(8, 62, 247, 101)) iRes = 1;
			if (GRectTouched(8, 105, 247, 144)) iRes = 2;
			if (GRectTouched(8, 148, 247, 187)) iRes = 3;
			if (GRectTouched(244, 182, 255, 191)) iRes = 4;
		}

		Sound(sok);
		if (IRC_MainStatusWindow == NULL)
		{
			PA_DeleteSprite(1, 0); PA_DeleteSprite(1, 1); PA_DeleteSprite(1, 2); PA_DeleteSprite(1, 3);
		}			
		BrightnessUp();
		PA_DeleteSprite(0, 0); PA_DeleteSprite(0, 1); PA_DeleteSprite(0, 2); PA_DeleteSprite(0, 3); PA_DeleteSprite(0, 4);
		switch (iRes)
		{
		   case 1: if (WiFi_Connect(1)) IRC_Start(); break;
		   case 2: if (WiFiConfig()) IRC_Start(); break;
		   case 3: Config_MainMenu(); break;
		   case 4: ShowAbout(); break;
		}   
	}

	return 0;
} // End of main()


